package news

import (
	"data/config"
	"data/view/auth"
	"data/view/auth/auth_const"
	"data/view/other"
	"data/view/user"
	"public/message"
	"public/mysqldb"
	"public/tools"
	"time"

	"github.com/ant0ine/go-json-rest/rest"
)

//编辑或新增新闻
func Action(w rest.ResponseWriter, r *rest.Request) {
	var req NewsActionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Title) || req.Action == 0 {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_11, auth_const.L_A, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var news News_info_tbl
	var err error
	orm.Where("title = ?", req.Title).Find(&news)

	news.Title = req.Title
	news.Content = req.Content
	news.Cover = req.Cover
	news.Operator = username

	if req.Action == 1 { //新增
		if news.Id > 0 {
			w.WriteJson(message.GetErrorMsg(message.ExistedError))
			return
		}
		news.Create_time = time.Now()
		err = orm.Create(&news).Error
	} else if req.Action == 2 { //修改
		if news.Id == 0 {
			w.WriteJson(message.GetErrorMsg(message.NotFindError))
			return
		}
		news.Title = req.Title_new
		err = orm.Table("news_info_tbl").Where("title = ?", req.Title).Updates(news).Error
	} else {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid))
		return
	}
	if err != nil {
		w.WriteJson(message.GetErrorMsg(message.UnknownError))
		return
	}
	w.WriteJson(message.GetSuccessMsg(message.NormalMessageId))
}

//删除新闻
func Delete(w rest.ResponseWriter, r *rest.Request) {
	var req NewsActionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Title) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_11, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var news News_info_tbl
	orm.Where("title = ?", req.Title).Find(&news)

	if news.Id == 0 {
		w.WriteJson(message.GetErrorMsg(message.NotFindError))
		return
	}
	err := orm.Delete(News_info_tbl{}, "title = ?", req.Title).Error
	if err != nil {
		w.WriteJson(message.GetErrorMsg(message.UnknownError))
		return
	}
	w.WriteJson(message.GetSuccessMsg(message.NormalMessageId))
}

//获取全部新闻
func GetAll(w rest.ResponseWriter, r *rest.Request) {
	var req NewsActionParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_11, auth_const.L_A, auth_const.L_B, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var news []News_info_tbl
	orm.Find(&news)
	pic := other.GetPic(NEWS_MODULE)
	mp := make(map[string]interface{})
	mp["news"] = news
	mp["pic"] = pic[NEWS_MODULE]
	msg := message.GetSuccessMsg(message.NormalMessageId)
	msg.Data = mp
	w.WriteJson(msg)
}

//编辑图片
func UpdatePic(w rest.ResponseWriter, r *rest.Request) {
	var req UpdatePicParam
	tools.GetRequestJsonObj(r, &req)

	//参数检测
	if !tools.CheckParam(req.Access_token, req.Pic) {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid, req))
		return
	}
	//验证token，并获取用户名
	username, _, bfind := user.GetUserFromToken(req.Access_token)
	if !bfind {
		w.WriteJson(message.GetErrorMsg(message.TokenFailure))
		return
	}
	u, bb := user.GetUserDetail(username)
	if !bb {
		w.WriteJson(message.GetErrorMsg(message.UserNotExisted))
		return
	}
	//权限检验
	b := auth.CheckRoleAuth(u.Business_role, auth_const.M_11, auth_const.L_A, auth_const.L_C)
	if !b {
		w.WriteJson(message.GetErrorMsg(message.UserMenuNotExisted))
		return
	}
	result := other.UpdatePicByModule(NEWS_MODULE, req.Pic)
	if !result {
		w.WriteJson(message.GetErrorMsg(message.UnknownError))
		return
	}
	w.WriteJson(message.GetSuccessMsg())
}
